package com.slofzx.nuoda.evaluation.service.impl;

import com.slofzx.nuoda.common.dto.RoleDTO;
import com.slofzx.nuoda.common.dto.UserDTO;
import com.slofzx.nuoda.common.dto.UserQueryDTO;
import com.slofzx.nuoda.common.entity.SysUser;
import com.slofzx.nuoda.common.entity.SysUserRole;
import com.slofzx.nuoda.common.enums.CacheTypeEnum;
import com.slofzx.nuoda.common.enums.CommonResultEnum;
import com.slofzx.nuoda.common.enums.ServiceResult;
import com.slofzx.nuoda.common.enums.SysPermission;
import com.slofzx.nuoda.common.exception.ServiceException;
import com.slofzx.nuoda.common.service.impl.SysUserServiceImpl;
import com.slofzx.nuoda.common.shiro.Principal;
import com.slofzx.nuoda.common.util.CacheUtil;
import com.slofzx.nuoda.common.util.IdGen;
import com.slofzx.nuoda.common.util.RSAUtil;
import com.slofzx.nuoda.common.vo.CompanyVO;
import com.slofzx.nuoda.common.vo.PermissionVO;
import com.slofzx.nuoda.common.vo.SysUserVO;
import com.slofzx.nuoda.evaluation.enums.PermissionEnum;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.cache.Cache;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 扩展用户维护逻辑，新增管理所有用户权限
 */
@Primary
@Service
public class CustomUserServiceImpl extends SysUserServiceImpl {


    public boolean hasPermission(SysUserVO userVO,SysPermission permission) {
        if(permission == null) {
            return true;
        }
        if(userVO.getPermissionList() == null||userVO.getPermissionList().isEmpty()){
            return false;
        }
        if(userVO.getPermissionList() == null) {
            return false;
        }
        for(PermissionVO item:userVO.getPermissionList()) {
            if(item.getId().equals(permission.getId())) {
                return true;
            }
        }
        return false;
    }


    /**
     * 获取指定用户可查看组织机构集合
     *
     * @param user
     * @return
     */
    public Set<String> getFilterOfficeSet(SysUserVO user,UserQueryDTO dto) {
        Set<String> officeIdSet = null;
        if (StringUtils.isNotEmpty(dto.getOfficeId()) || user.isAdmin() == false&&hasPermission(user,PermissionEnum.SYS_USER_MANAGE_ALL) == false) {
            //如果不是超级管理员，则只可查看本公司及下属单位数据
            officeIdSet = companyService.getSubOfficeIdSet(user.getCompanyId());
            // 有筛选条件时先查看是否有该组织机构权限
            if (officeIdSet.contains(dto.getOfficeId())) {
                officeIdSet = companyService.getSubOfficeIdSet(dto.getOfficeId());
            }
            officeIdSet.add(dto.getOfficeId());
            dto.setOfficeId(null);
        }
        return officeIdSet;
    }
    @Override
    public List<SysUserVO> findDataList(UserQueryDTO dto) {
        SysUserVO currentUser = this.getCurrentUser();
        if (!dto.isGlobal()) {
            dto.setFilterOfficeSet(this.getFilterOfficeSet(currentUser,dto));
        }

        if (StringUtils.isNotEmpty(dto.getName())) {
            dto.setName("%" + dto.getName().replace("%", "\\%").replace("_", "\\_") + "%");
        }

        if (StringUtils.isNotEmpty(dto.getLoginName())) {
            dto.setLoginName("%" + dto.getLoginName().replace("%", "\\%").replace("_", "\\_") + "%");
        }

        List<SysUser> userList = this.sysUserDAO.findList(dto);
        if (userList != null && !userList.isEmpty()) {
            Map<String, Object> params = new HashMap();
            Set<String> userIdSet = new HashSet();
            Iterator var6 = userList.iterator();

            while(var6.hasNext()) {
                SysUser user = (SysUser)var6.next();
                userIdSet.add(user.getId());
            }

            params.put("userIds", userIdSet);
            List<SysUserVO> resultList = this.sysUserDAO.findListWithRole(params);
            return resultList;
        } else {
            return Collections.emptyList();
        }
    }


}
